The goal of this lab is to explore static tables using the grammar of tables from the gt and gtExtras package.
The gt() function creates a gt table object when provided with table data. Using this function is the first step in a typical gt workflow. Once we have the gt table object, we can perform styling transformations by adding on column, formatting, tab styling, and summary layers.
A few helpful links to provide more information about the gt package: gt rstudio and intro to gt
Posit (formally RStudio) hosts an annual table competition! Check out some past winning tables: 20212022
Dataset
We will be using the pizzaplace dataset included in the gt package, the house_prices, stream_data, and team_logo dataset which should be located in your /data subdirectory. Each dataset will require a bit of data wrangling provided in their respective exercise code chunk.
# load package(s)library(tidyverse)library(gt)library(gtExtras)# load data# exists w/ gt library - data() puts it into environmentdata("pizzaplace")# load paces and stores rda file in environmentload("data/house_prices.rda")# reads data, the arrow stores it in environmentstream_data <-read_csv("data/stream_data.csv", show_col_types =FALSE) |> janitor::clean_names()# similar to csvteam_logo <-read_rds("data/team_logo.rds") |> janitor::clean_names()
Exercise 1
Use the pizza_summary dataset created below to recreate the following graphic as precisely as possible. We will work through this exercise together to provide a high level overview of a few of the ways you can layer on table elements to make a simple but visually appealing table.
Hints:
md(): Interpret input text as Markdown-formatted text
base_pizza <- pizza_size |>gt(# what each row w/in a group will berowname_col ="size",# the groups that rowname_col will be for eachgroupname_col ="category" ) |>tab_header(title =md("**Pizza Place Annual Pizza Sales**"),subtitle ="Fiscal Year 2015" )
Code
base_pizza |># have to do a different tab_style for each rowtab_style(style =list(cell_fill(color ="#F8766D"),cell_text(color ="white")),locations =cells_body(rows = size =="S") ) |>tab_style(style =list(cell_fill(color ="#00BA38"),cell_text(color ="black", style ="italic")),locations =cells_body(rows = size =="M") ) |>tab_style(style =list(cell_fill(color ="#619CFF"),cell_text(color ="black", weight ="bold")),locations =cells_body(rows = size =="L") )
Pizza Place Annual Pizza Sales
Fiscal Year 2015
number_sold
profit
chicken
L
4932
102339.00
M
3894
65224.50
S
2224
28356.00
classic
L
4057
74518.50
M
4112
60581.75
S
6139
69870.25
veggie
L
5403
104202.70
M
3583
57101.00
S
2663
32386.75
Table 2
Hints:
gt_color_rows is useful for changing entire column colors
Using the house_illinois dataset derived from the house_price dataset, recreate the following graphic as precisely as possible.
Similar to the ggplot scales_*() function, the gt package has useful transformations that can be performed using the following syntax tab_*(), fmt_*(), cols_*(). Check the package documentation for a full list of options.
house_illinois |>gt(# this removes the label of the columnrowname_col ="year" ) |>tab_header(title ="Average Annual Housing Price Index (HPI)",subtitle =md("*Illinois Prices for 2006-2010*") ) |>tab_style(style =cell_text(color ="red"),location =cells_body(columns = change,# this is the check for if it's red or notrows = change <0 ) ) |>fmt_currency(columns = hpi,currency ="USD",decimals =2 ) |>fmt_percent(columns = change,decimals =2,# gives the same number of significant digitsscale_values =TRUE,# commas and () for negativeaccounting =TRUE ) |>tab_spanner(# the name of the new column grouplabel ="Housing Price Index",# the columns the group is made forcolumns =c(hpi, change) ) |>cols_label(hpi ="Value",change ="% Change" ) |>tab_footnote(footnote ="Housing market crash",locations =cells_stub(rows ="2008") )
Average Annual Housing Price Index (HPI)
Illinois Prices for 2006-2010
Housing Price Index
Value
% Change
2005
$138.39
7.81%
2006
$145.69
5.31%
2007
$145.54
(0.09%)
1 2008
$135.06
(7.22%)
2009
$122.30
(9.40%)
2010
$116.27
(4.93%)
1 Housing market crash
Exercise 4
Adding icons and graphics to a table is made easy with gtExtras. Using the stream_data dataset, recreate the following graphic as precisely as possible.
Hints:
gt_plt_bullet()
gt_fa_column()
nytimes theme()
fmt_symbol_first()
Solution
Code
# pipe the following code into your gt functionsstream_data |>mutate(type =map(type, fontawesome::fa) ) |>gt() |>gt_theme_nytimes() |>gt_plt_bullet(# column is the barcolumn = nominee,# target is the actual bullet value that it would put on top of nomineetarget = winner,# pallette = c(bar color, bullet color). default is grey and redpalette =c("grey", "blue") ) |>fmt_symbol_first(column = ratio,suffix ="%",decimals =1,scale_by =100 ) |>tab_header(title =md("**Netflix leads Emmy Nominations & Wins:<br>2020-2021**"),subtitle =md("but **HBO** leads in their Wins to Nominations ratio") ) |>gt_highlight_rows(# this tells which variable it looks through, then it looks through distributor for HBO and highlights thatrows = distributor =="HBO",fill ="grey",alpha =0.4 ) |>gt_add_divider(columns = type,sides ="right",color ="lightgrey",weight =px(1) ) |>cols_width( distributor ~px(100), type ~px(40), ratio ~px(60), nominee ~px(230) )
Netflix leads Emmy Nominations & Wins: 2020-2021
but HBO leads in their Wins to Nominations ratio
distributor
type
ratio
nominee
Netflix
27.7%
HBO
28.8%
NBC
20.5%
ABC
16.0%
Hulu
2.0%
FX Networks
11.1%
Prime Video
8.9%
Apple TV+
23.8%
CBS
2.5%
Exercise 5
Using the team_logo dataset, recreate the following graphic as precisely as possible.
The tab_options() layer for gt is similar to the theme() option for ggplot. Here you can add customization to the background colors, line type, size, etc.
Hints:
gt_img_rows with height = 30
tab options:
data row padding is px(1)
table background color is "gray90"
table width is 100%
table font color is "red"
column labels have background color "black", font size "large", and font weight "bold",
table body has hlines color "red"
table body has border bottom color "red"
Solution
Code
# pipe the following code into your gt functionsteam_logo |> dplyr::select(logo = team_logo_espn, team_name, team_nick, team_conf) |>slice_sample(n =2) |>gt() |>gt_img_rows(columns = logo,img_source ="web",height =30 ) |>tab_options(data_row.padding =px(1),table.width ="100%",table.background.color ="gray90",table.font.color ="red",column_labels.background.color ="black",column_labels.font.size ="large",column_labels.font.weight ="bold",table_body.hlines.color ="red",table_body.border.bottom.color ="red" )